import json
import random
import time
from urllib import request

import numpy as np
from bs4 import BeautifulSoup

import mysql.connector
from fdfs_client.client import get_tracker_conf, Fdfs_client


def initMysql():
  global mydb
  global mycursor

  mydb = mysql.connector.connect(
    host='8.129.219.66',
    user='guifangtong',
    passwd='guifangtong1234!@#',
    database='guifangtong'
  )

  # mydb = mysql.connector.connect(
  #   host='localhost',
  #   user='root',
  #   passwd='root1234',
  #   database='guifangtong'
  # )

  mycursor = mydb.cursor()


def uploadPic(picurl):
  try:
    tracker_path = get_tracker_conf('./client.conf')
    client = Fdfs_client(tracker_path)
    # ret_upload
    # ret_upload = client.upload_by_filename('D://12.jpg') # 图片地址
    resp = request.urlopen(picurl)
    # image = np.asarray(bytearray(resp.read()), dtype="uint8")
    ret_upload = client.upload_by_buffer(resp.read())  # 图片地址
    # print(ret_upload['Remote file_id'])
    # print(ret_upload)
    return ret_upload['Remote file_id']
  except Exception as e:
    print('连接失败', e)


#更加价格
def updatePrice(layout_obj):
  initMysql()
  table_name = 't_fang'
  price =  layout_obj['sale_price']
  # print('price',str(price))
  cmd_sql = 'UPDATE ' + table_name + ' set sale_price = "' + str(price) + '" where feature = ' + str(layout_obj['id'])
  print("更新sql", cmd_sql)
  try:
    mycursor.execute(cmd_sql)
    mydb.commit()
    print('更新成功了')
  except Exception as e:
    print('更新出错了', e)
    mydb.rollback()  # 发生错误时回滚
    mydb.close()


def sousuoLgbHx(fang_hengbao_id):
  url = 'https://hfb.hengfangbao.cn/api/public/biz_item?item_id=' + fang_hengbao_id
  print('请求地址', url)
  # 直接请求
  response = request.urlopen(url)
  context = response.read().decode('utf-8')
  data = json.loads(context)
  if data['code'] == 1:
    layout_obj = data['result']
    # updateFangXq(layout_obj)
    # updateFangXqForLnglat(layout_obj)
    # updateFangFacePic(layout_obj)
    updatePrice(layout_obj)
  else:
    print(data['msg'])


def findFangs():
  initMysql()
  table_name = 't_fang'
  sql = "select * from " + table_name
  mycursor.execute(sql)
  myresult = mycursor.fetchall()  # fetchall() 获取所有记录
  return myresult

def updateFangFacePic(layout_obj):
    initMysql()
    table_name = 't_fang'
    pic = ''
    thumb = layout_obj['thumb']
    if thumb != '':
      pic = uploadPic(thumb).decode('utf-8')
    else:
      print('没有图片')

    cmd_sql = 'UPDATE ' + table_name + ' set face_pic = "'+pic+'" where feature = ' + str(layout_obj['id'])
    print('----------------华丽分割线start--------------------------------')
    print("更新sql", cmd_sql)
    try:
      mycursor.execute(cmd_sql)
      mydb.commit()
      print('更新成功了')
    except Exception as e:
      mydb.rollback()  # 发生错误时回滚
      print('更新出错了', e)
    mydb.close()
    print('----------------华丽分割线end--------------------------------')

if __name__ == '__main__':
  resFangs = findFangs()
  for item in resFangs:
    hf_fangid = item[12]
    sousuoLgbHx(hf_fangid)
    time.sleep(2)
